package com.xcity.game.combat.attr;

import com.alibaba.fastjson.annotation.JSONField;

public class CombatAttribute implements CombatAttributeFilter, CombatAttributeFormatter {

	@JSONField(name = "0")
	protected CombatAttributeType type;
	@JSONField(name = "1")
	protected float value;
	@JSONField(serialize = false)
	protected CombatAttributeFilter filter;
	@JSONField(serialize = false)
	protected CombatAttributeFormatter formatter;
	
	public CombatAttribute() {
	}
	
	public CombatAttribute(CombatAttributeType type, float value) {
		this(type, value, null, null);
	}
	
	public CombatAttribute(CombatAttributeType type, float value, CombatAttributeFilter filter, CombatAttributeFormatter formatter) {
		if (value <= 0) {
			throw new IllegalArgumentException("Combat attribute error: " + type + "=" + value);
		}
		this.type = type;
		this.value = value;
		this.filter = filter;
		this.formatter = formatter;
	}

	public CombatAttributeType getType() {
		return type;
	}

	public void setType(CombatAttributeType type) {
		this.type = type;
	}

	public float getValue() {
		return value;
	}

	public void setValue(float value) {
		this.value = value;
	}
	
	public CombatAttribute clone() {
		return new CombatAttribute(type, value, filter, formatter);
	}

	@Override
	public float doFilter(float value, float factor) {
		return filter != null ? filter.doFilter(value, factor) : type.doFilter(value, factor);
	}

	@Override
	public Object format(float value) {
		return formatter != null ? formatter.format(value) : type.format(value);
	}
	
}
